package ar.uade.distribuidas.tpo.dba.servicio;

import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;



import ar.uade.distribuidas.tpo.base.remoto.IGestion;


public class BS {
	public void publicarServicio() {
    	try {
    		LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
    		
    		IGestion stubGestionUnificada = Gestion.getInstancia();
			Naming.rebind("//localhost/Gestion", stubGestionUnificada);
    	
    		//this.cerrar();
    		/*IGestionRodamientos stubRodamientos = new GestionRodamiento();
			Naming.rebind("//localhost/GestionRodamientos", stubRodamientos);
			
			IGestionSolicitudDeCotizacion stubSolicitudDeCotizacion = new GestionSolicitudDeCotizacion();
			Naming.rebind("//localhost/GestionSolicitudDeCotizacion", stubSolicitudDeCotizacion);
			
			IGestionCotizacion stubCotizacion = new GestionCotizacion();
			Naming.rebind("//localhost/GestionCotizacion", stubCotizacion);
			
			IGestionListaPrecio stubListaPrecio = new GestionListaPrecio();
			Naming.rebind("//localhost/GestionListaPrecio", stubListaPrecio);
		
			IGestionCliente stubCliente = GestionCliente.getInstancia();
			//IGestionCliente stub = (IGestionCliente) UnicastRemoteObject.exportObject(stubCliente, 0);
			Naming.rebind("//localhost/GestionCliente", stubCliente);
			
			IGestionStock stubStock = new GestionStock();
			Naming.rebind("//localhost/GestionStock", stubStock);
			verVinculos();
			
			IGestionProveedor stubProveedor = new GestionProveedor();
			Naming.rebind("//localhost/GestionProveedor", stubProveedor);
			
			IGestionOC stubOrdenCompra = new GestionOC();
			Naming.rebind("//localhost/GestionOrdenCompra", stubOrdenCompra);
			
			*/
			verVinculos();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public void verVinculos() {
        try {
      	  String[] vinculos = Naming.list( "" );
      	  System.out.print(InetAddress.getLocalHost().getHostAddress());
      	  for ( int i = 0; i < vinculos.length; i++ ){
      		System.out.println( vinculos[i] );
      	  }
      	  System.out.print( ">>> Servicio DBA publicado");
        }
        catch (Exception e) {
      	  e.printStackTrace();
        }
     }
    
    public void cerrar() {
		try {
			Naming.unbind("Gestion");
		} catch (Exception e) {
		} finally {
			System.exit(0);
		}
    }
}


